Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration

Public Partial Class MovieTixStaff_adduser
	Inherits System.Web.UI.Page
	Protected Sub Page_Load(sender As Object, e As EventArgs)

    End Sub

    Public Sub function1(ByVal source As Object, ByVal args As ServerValidateEventArgs)

        Dim username As String = UsernameTextbox.Text
        Label2.Text = "1"

        Dim connectionString As String
        connectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim con As New SqlConnection(connectionString)
        Dim cmd1 As New SqlCommand("SELECT TheatreStaffUserName,TheatreId from TheatreStaff WHERE TheatreStaffUserName=@TheatreStaffUserName", con)

        Dim Showing As [Boolean]

        cmd1.Parameters.AddWithValue("TheatreStaffUserName", username)
        Using con
            con.Open()
            Dim reader As SqlDataReader = cmd1.ExecuteReader()
            If reader.HasRows Then
                Label2.Text = "1"
                args.IsValid = False


                Showing = False
            Else
                Label2.Text = "2"
                Showing = True
                args.IsValid = True
            End If
        End Using
    End Sub
    Public Sub Clear(ByVal sender As Object, ByVal args As EventArgs)
        Label1.Text = " "


    End Sub



    Public Sub CreateUser_OnClick(ByVal sender As Object, ByVal args As EventArgs)

        Dim username As String = UsernameTextbox.Text
        Label2.Text = "1"

        Dim connectionString1 As String
        connectionString1 = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim con1 As New SqlConnection(connectionString1)
        Dim cmd1 As New SqlCommand("SELECT TheatreStaffUserName,TheatreId from TheatreStaff WHERE TheatreStaffUserName=@TheatreStaffUserName", con1)

        Dim Showing As [Boolean]

        cmd1.Parameters.AddWithValue("TheatreStaffUserName", username)
        Using con1
            con1.Open()
            Dim reader As SqlDataReader = cmd1.ExecuteReader()
            If reader.HasRows Then
                Label2.Text = "1"



                Showing = False
            Else
                Label2.Text = "2"

                Showing = True
            End If
        End Using


        If Showing = True Then


            Dim result As MembershipCreateStatus

            Try
                ' Create new user.

                If Membership.RequiresQuestionAndAnswer Then
                    Dim newUser As MembershipUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text, EmailTextbox.Text, PasswordQuestionTextbox.Text, PasswordAnswerTextbox.Text, True, _
                     result)


                    If Roles.RoleExists("Theatreadmin") Then

                        If Not Roles.IsUserInRole(UsernameTextbox.Text, "Theatreadmin") Then

                            Roles.AddUserToRole(UsernameTextbox.Text, "Theatreadmin")
                        End If
                    End If
                    Dim connectionString As String
                    connectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

                    Dim con As New SqlConnection(connectionString)
                    Dim cmd As New SqlCommand("INSERT TheatreStaff(TheatreStaffUserName,TheatreStaffAcessLevel,TheatreId) values (@UName,@ALevel,@Id)", con)
                    cmd.Parameters.AddWithValue("@UName", UsernameTextbox.Text)
                    cmd.Parameters.AddWithValue("@ALevel", "Admin")
                    cmd.Parameters.AddWithValue("@Id", theatre2.SelectedItem.Value)

                    Label1.Text = "The New user has been added sucessfully to the theatre!"

                    Using con
                        con.Open()
                        Dim rowCount As Int32 = cmd.ExecuteNonQuery()

                        If rowCount > 0 Then
                            Label1.Text = "The New user [" + UsernameTextbox.Text + "] has been added sucessfully to the theatre " + theatre2.SelectedItem.Text

                            UsernameTextbox.Text = ""
                            PasswordTextbox.Text = ""
                            EmailTextbox.Text = ""
                            PasswordQuestionTextbox.Text = ""



                            PasswordAnswerTextbox.Text = ""

                        End If
                    End Using
                Else
                    Dim newUser As MembershipUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text, EmailTextbox.Text)
                    If Roles.RoleExists("Theatreadmin") Then

                        If Not Roles.IsUserInRole(UsernameTextbox.Text, "Theatreadmin") Then

                            Roles.AddUserToRole(UsernameTextbox.Text, "Theatreadmin")
                        End If
                    End If
                    Dim connectionString As String
                    connectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

                    Dim con As New SqlConnection(connectionString)
                    Dim cmd As New SqlCommand("INSERT TheatreStaff(TheatreStaffUserName,TheatreStaffAcessLevel,TheatreId) values (@UName,@ALevel,@Id)", con)
                    cmd.Parameters.AddWithValue("@UName", UsernameTextbox.Text)
                    cmd.Parameters.AddWithValue("@ALevel", "Admin")
                    cmd.Parameters.AddWithValue("@Id", theatre2.SelectedItem.Value)


                    Using con
                        con.Open()

                        'if (rowCount > 0)
                        '{
                        '    lresult.Text = "The New user has been added sucessfully to the theatre!";
                        '}

                        Dim rowCount As Int32 = cmd.ExecuteNonQuery()
                    End Using

                    'Response.Redirect("login.aspx");
                End If
            Catch e As MembershipCreateUserException
                Msg.Text = GetErrorMessage(e.StatusCode)
            Catch e As HttpException
                Msg.Text = e.Message

            End Try
        End If
    End Sub

    Public Function GetErrorMessage(ByVal status As MembershipCreateStatus) As String
        Select Case status
            Case MembershipCreateStatus.DuplicateUserName
                Return "Username already exists. Please enter a different user name."

            Case MembershipCreateStatus.DuplicateEmail
                Return "A username for that e-mail address already exists. Please enter a different e-mail address."

            Case MembershipCreateStatus.InvalidPassword
                Return "The password provided is invalid. Please enter a valid password value."

            Case MembershipCreateStatus.InvalidEmail
                Return "The e-mail address provided is invalid. Please check the value and try again."

            Case MembershipCreateStatus.InvalidAnswer
                Return "The password retrieval answer provided is invalid. Please check the value and try again."

            Case MembershipCreateStatus.InvalidQuestion
                Return "The password retrieval question provided is invalid. Please check the value and try again."

            Case MembershipCreateStatus.InvalidUserName
                Return "The user name provided is invalid. Please check the value and try again."

            Case MembershipCreateStatus.ProviderError
                Return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator."

            Case MembershipCreateStatus.UserRejected
                Return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator."
            Case Else

                Return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator."
        End Select
    End Function

End Class
